/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.math.BigDecimal;
import java.sql.Timestamp;
import org.ofbiz.entity.*;
import org.ofbiz.entity.condition.*;
import org.ofbiz.entity.util.*;
import org.ofbiz.base.util.*;
import org.ofbiz.order.order.*;
import org.ofbiz.party.contact.*;
import org.ofbiz.product.inventory.InventoryWorker;
import org.ofbiz.product.catalog.CatalogWorker;
import org.ofbiz.accounting.payment.*;

import javolution.util.FastMap;

restaurantName = null;
taxId = null;
vat = null;

partyGroup = delegator.findOne("PartyGroup", [partyId: "RESTAURANT"], false);
restaurantName = partyGroup.groupName;
taxList = delegator.findByAnd("PartyTaxAuthInfo", [partyId: "RESTAURANT", taxAuthGeoId: "_NA_", taxAuthPartyId: "RESTAURANT_TAX"], null, false);
taxList = EntityUtil.filterByDate(taxList);
if (taxList) {
    taxId = taxList[0].partyTaxId;
}
vatList = delegator.findByAnd("TaxAuthorityRateProduct", [taxAuthPartyId: "RESTAURANT_TAX", taxAuthorityRateTypeId: "VAT_TAX"], null, false);
vatList = EntityUtil.filterByDate(vatList);
if (vatList) {
    vat = vatList[0].taxPercentage
}
context.restaurantName = restaurantName;
context.taxId = taxId;
context.vat = vat;

printResult = dispatcher.runSync("rGetPrinterList", [:]);
context.printerList = printResult.printerList;

webSiteAttributeList = delegator.findByAnd("WebSiteAttribute", ["webSiteId": context.webSiteId], null, false);
cashierPrinter = null;
kitchenPrinter = null;
openTime = UtilDateTime.nowTimestamp();
openTimeHour = 0;
openTimeMinute = 0;
closeTime = UtilDateTime.nowTimestamp();
closeTimeHour = 0;
closeTimeMinute = 0;
for (GenericValue webSiteAttribute : webSiteAttributeList) {
    if (webSiteAttribute.attrName == "CASHIER_PRINTER") {
        cashierPrinter = webSiteAttribute.attrValue;
    } else if (webSiteAttribute.attrName == "KITCHEN_PRINTER") {
        kitchenPrinter = webSiteAttribute.attrValue;
    } else if (webSiteAttribute.attrName == "openTime") {
        openTime = UtilDateTime.stringToTimeStamp(webSiteAttribute.getString("attrValue"), "yyyy-mm-dd hh:mm:ss", timeZone, locale);
		openTimeHour = openTime.getHours();
		openTimeMinute = openTime.getMinutes();
    } else if (webSiteAttribute.attrName == "closeTime") {
        closeTime = UtilDateTime.stringToTimeStamp(webSiteAttribute.getString("attrValue"), "yyyy-mm-dd hh:mm:ss", timeZone, locale);
		closeTimeHour = closeTime.getHours();
		closeTimeMinute = closeTime.getMinutes();
    }
}
context.cashierPrinter = cashierPrinter;
context.kitchenPrinter = kitchenPrinter;
context.openTime = openTime;
context.openTimeHour = openTimeHour;
context.openTimeMinute = openTimeMinute;
context.closeTime = closeTime;
context.closeTimeHour = closeTimeHour;
context.closeTimeMinute = closeTimeMinute;